frappe.ui.form.on('Tongtool Order', {
    refresh: function(frm) {
        // 添加刷新订单按钮
        if (frm.doc.raw_data && !frm.doc.__islocal) {
            frm.add_custom_button(__('Refresh Order Data'), function() {
                frappe.call({
                    method: 'tongtool_integration.sync.order_sync.refresh_single_order',
                    args: {
                        order_name: frm.doc.name
                    },
                    callback: function(r) {
                        if (r.message && r.message.success) {
                            frm.reload_doc();
                        }
                    }
                });
            }, __('Tongtool Actions'));
        }
        
        // 添加重新匹配SKU按钮
        if (!frm.doc.__islocal) {
            frm.add_custom_button(__('Rematch SKU'), function() {
                frappe.call({
                    method: 'tongtool_integration.sync.order_sync.rematch_sku_for_orders',
                    args: {
                        order_names: JSON.stringify([frm.doc.name])
                    },
                    callback: function(r) {
                        if (r.message && r.message.success) {
                            frm.reload_doc();
                        }
                    }
                });
            }, __('Tongtool Actions'));
        }
    }
});
